<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tool :tree-node="company" :is-boot="true" @addDept="addDept" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tool slot-scope="{ data }" :tree-node="data" @editDept="editDept" @addDept="addDept" @delDepts="getDepart" />
        </el-tree>
      </el-card>
      <add-dept ref="addDept" :is-dialog.sync="isDialog" :tree-node="treeDept" @addDepts="getDepart" />
    </div>
  </div>
</template>

<script>
import TreeTool from './component/tree-tool.vue'
import AddDept from './component/add-dept.vue'
import { getDepartment } from '@/api/department.js'
import { transDataList } from '@/utils'
export default {
  components: {
    TreeTool,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      company: {},
      isDialog: false,
      treeDept: null
    }
  },
  created() {
    this.getDepart()
  },
  methods: {
    async getDepart() {
      const result = await getDepartment()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transDataList(result.depts, '')
      console.log(result)
    },
    addDept(node) {
      this.isDialog = true
      this.treeDept = node
    },
    editDept(node) {
      this.isDialog = true
      this.node = node
      console.log(this.$refs.addDept)
      this.$refs.addDept.getUserDepartment(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card{
    padding: 30px 140px;
    font-size: 14px;
}
</style>
